<template>
  <div class="dashboard-container">
    <div class="dashboard-text" style="min-height: 695px;">
      <el-row style="margin-bottom: 15px;line-height: 30px;">
        <el-col :span="12">
          <span style="font-size: 18px;font-weight: bold;">用户管理</span>
        </el-col>
        <el-col :span="12" style="text-align: right;">
          <el-button type="primary" style="width: 13%;" @click="dialogFormVisible = true">创建用户</el-button>
        </el-col>
      </el-row>
      <!-- 需要封装开始 -->
      <el-dialog title="新增用户" :visible.sync="dialogFormVisible" class="tjcd">
        <el-form :model="form" :rules="rules">
          <el-form-item label="登录账号：" prop="Rolename" :label-width="formLabelWidth">
            <el-input v-model="form.adminUsername" placeholder="请输入账号" :disabled="false" autocomplete="off" />
          </el-form-item>
          <el-form-item label="登录密码：" prop="Loginpassword" :label-width="formLabelWidth">
            <el-input v-model="form.adminPassword" type="password" auto-complete="new-password" autocomplete="off" :disabled="edit" placeholder="请输入密码" show-password />
          </el-form-item>
          <el-form-item label="所属上级：" prop="Loginpassword" :label-width="formLabelWidth">
            <el-select v-model="value" style="width: 100%;" placeholder="请选择用户所属父级">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="显示姓名：" prop="fullname" :label-width="formLabelWidth">
            <el-input v-model="form.adminFullname" placeholder="请输入姓名" />
          </el-form-item>
          <el-form-item label="电话号码：" prop="Telephonenumber" :label-width="formLabelWidth">
            <el-input v-model="form.adminPhone" placeholder="请输入电话号码" />
          </el-form-item>
          <el-form-item label="分配角色：" prop="Telephonenumber" :label-width="formLabelWidth">
            <el-select v-model="value" style="width: 100%;" placeholder="请选择用户所属父级">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="是否启用：" prop="emailaddress" :label-width="formLabelWidth">
            <el-switch v-model="form.adminStatus" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 需要封装结束 -->
      <el-row>
        <el-col :span="24">
          <el-table
            :data="tableData"
            border
            style="width: 100%"
          >
            <el-table-column
              prop="id"
              align="center"
              label="编号"
            />
            <el-table-column
              prop="adminUsername"
              align="center"
              label="登录账号"
            />
            <el-table-column
              prop="adminFullname"
              align="center"
              label="姓名"
            />
            <el-table-column
              prop="adminFullname"
              align="center"
              label="邮箱"
            />
            <el-table-column
              prop="adminPhone"
              align="center"
              label="电话号码"
            />
            <el-table-column
              prop="adminCreatetime"
              align="center"
              label="添加时间"
            />
            <el-table-column
              prop="adminLastlogintime"
              align="center"
              label="最后登录"
            />
            <el-table-column
              prop="name"
              align="center"
              label="是否启用"
            >
              <el-switch v-model="value1" />
            </el-table-column>
            <el-table-column
              prop="address"
              align="center"
              label="设置"
            >
              <el-button type="text" size="small">查看下级</el-button>
            </el-table-column>
            <el-table-column
              label="操作"
              align="center"
            >
              <template slot-scope="scope">
                <el-button type="text" size="small" @click="handleClick(scope.row)">分配角色</el-button>
                <el-button type="text" size="small">编辑</el-button>
                <el-button type="text" size="small" @click="handleClickDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <el-row style="margin: 25px 0 15px;line-height: 26px;text-align: right;">
        <el-col :span="24">
          <div class="block">
            <el-pagination
              :current-page="currentPage4"
              :page-size="100"
              layout="total, prev, pager, next"
              :total="400"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getUserList, addUser, deleteUser } from '../../api/user.js'
export default {
  name: 'Dashboard',
  data() {
    return {
      dialogFormVisible: false,
      form: {
        adminUsername: '', // 用户账号
        adminPassword: '', // 用户密码
        pid: '',
        adminFullname: '', // 用户户名称
        adminPhone: '',
        adminJs: '', // 角色id
        adminStatus: true // 是否启用
      },
      rules: {
        Rolename: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        Loginpassword: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        fullname: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        Telephonenumber: [
          { required: true, message: '请输入电话号码', trigger: 'blur' }
        ],
        emailaddress: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' }
        ]
      },
      formLabelWidth: '100px',
      value1: true,
      currentPage4: 1,
      tableData: [],
      input: '',
      options: [{
        value: '选项1',
        label: '黄金糕'
      }, {
        value: '选项2',
        label: '双皮奶'
      }, {
        value: '选项3',
        label: '蚵仔煎'
      }, {
        value: '选项4',
        label: '龙须面'
      }, {
        value: '选项5',
        label: '北京烤鸭'
      }],
      value: ''
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    handeClick(row) {
      console.log(row)
    },
    // s删除用户
    handleClickDelete(val) {
      var data = {
        userIds: val.id
      }
      deleteUser(data).then(response => {
      }).catch(() => {
      })
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
    },
    // 获取用户列表
    getList() {
      getUserList(this.loginForm).then(response => {
        this.tableData = response.page.list
      }).catch(() => {
      })
    },
    // 添加用户
    addUser() {
      var data = {
        adminUsername: '', // 用户账号
        adminPassword: '', // 用户密码
        pid: '',
        adminFullname: '', // 用户户名称
        adminPhone: '',
        adminJs: '', // 角色id
        adminStatus: false // 是否启用
      }
      addUser(data).then(response => {
      }).catch(() => {
      })
    }
  },
  computed: {
    ...mapGetters([
      'name'
    ])
  }
}
</script>
<style>
  .tjcd /deep/ .el-dialog__header{
    line-height: 0 !important;
  }
  .tjcd /deep/ .el-dialog__body{
    padding-top: 15px !important;
  }
  .tjcd /deep/ .el-dialog{
    width: 30% !important;
  }
</style>
<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 15px;
  }
  &-text {
    padding: 15px;
    font-size: 30px;
    line-height: 46px;
    background-color: #fff;
  }
}
</style>
